'use strict'
const path = require('path')
const defaultSettings = require('./src/settings.js')

function resolve(dir) {
  return path.join(__dirname, dir)
}

const name = defaultSettings.title || '绿度银行管理平台'
const port = process.env.port || process.env.npm_config_port || 9528

module.exports = {
  publicPath: '/',
  outputDir: 'dist',
  assetsDir: 'static',
  lintOnSave: false,
  productionSourceMap: false,
  
  devServer: {
    port: port,
    open: true,
    overlay: {
      warnings: false,
      errors: true
    },
    // 禁用热重载以减少内存使用
    hot: false,
    liveReload: false,
    stats: 'errors-only'
  },
  
  configureWebpack: {
    name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    },
    
    // 优化配置
    optimization: {
      removeAvailableModules: true,
      removeEmptyChunks: true,
      mergeDuplicateChunks: true,
      splitChunks: {
        chunks: 'all',
        cacheGroups: {
          vendor: {
            name: 'chunk-vendors',
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: 'initial'
          },
          elementUI: {
            name: 'chunk-elementUI',
            priority: 20,
            test: /[\\/]node_modules[\\/]_?element-ui(.*)/
          }
        }
      }
    }
  },
  
  chainWebpack(config) {
    // 删除所有非必要的插件
    config.plugins.delete('preload')
    config.plugins.delete('prefetch')
    config.plugins.delete('fork-ts-checker')
    config.plugins.delete('friendly-errors')
    
    // 简化模块解析
    config.resolve.symlinks(false)
    
    // 禁用性能提示
    config.performance.hints(false)
    
    // 简化SVG处理
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()
      
    // 开发环境最小化配置
    config.when(process.env.NODE_ENV === 'development', config => {
      // 使用最简单的source map
      config.devtool('eval')
      // 最小化统计信息
      config.stats('errors-only')
    })
    
    // 设置模块解析缓存
    config.resolve.cacheWith(false)
  }
}